package src;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CardDialog extends JDialog {
    private JTextField cardNumberField;
    private JComboBox<String> depositTypeCombo;
    private JTextField openDateField;
    private JTextField openAmountField;
    private JTextField balanceField;
    private JPasswordField passwordField;
    private JCheckBox isLostCheckBox;
    private JTextField customerIdField;
    private boolean confirmed = false;

    public CardDialog(Frame owner, String title) {
        super(owner, title, true);
        setSize(400, 400);
        setLocationRelativeTo(owner);

        JPanel panel = new JPanel(new GridLayout(9, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 添加输入字段
        panel.add(new JLabel("卡号:"));
        cardNumberField = new JTextField();
        panel.add(cardNumberField);

        panel.add(new JLabel("存款类型:"));
        depositTypeCombo = new JComboBox<>(new String[]{"活期", "定期"});
        panel.add(depositTypeCombo);

        panel.add(new JLabel("开户日期:"));
        openDateField = new JTextField();
        openDateField.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        panel.add(openDateField);

        panel.add(new JLabel("开户金额:"));
        openAmountField = new JTextField();
        panel.add(openAmountField);

        panel.add(new JLabel("余额:"));
        balanceField = new JTextField();
        panel.add(balanceField);

        panel.add(new JLabel("密码:"));
        passwordField = new JPasswordField();
        panel.add(passwordField);

        panel.add(new JLabel("是否挂失:"));
        isLostCheckBox = new JCheckBox();
        panel.add(isLostCheckBox);

        panel.add(new JLabel("客户编号:"));
        customerIdField = new JTextField();
        panel.add(customerIdField);

        // 添加按钮
        JButton confirmButton = new JButton("确认");
        JButton cancelButton = new JButton("取消");

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
        panel.add(buttonPanel);

        // 添加事件监听器
        confirmButton.addActionListener(e -> {
            if (validateInput()) {
                confirmed = true;
                dispose();
            }
        });

        cancelButton.addActionListener(e -> dispose());

        add(panel);
    }

    private boolean validateInput() {
        if (cardNumberField.getText().trim().isEmpty() ||
            openDateField.getText().trim().isEmpty() ||
            openAmountField.getText().trim().isEmpty() ||
            balanceField.getText().trim().isEmpty() ||
            new String(passwordField.getPassword()).trim().isEmpty() ||
            customerIdField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有必填字段！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        try {
            double openAmount = Double.parseDouble(openAmountField.getText().trim());
            double balance = Double.parseDouble(balanceField.getText().trim());
            if (openAmount < 0 || balance < 0) {
                JOptionPane.showMessageDialog(this, "金额不能为负数！", "错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "金额格式不正确！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        return true;
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public String getCardNumber() {
        return cardNumberField.getText().trim();
    }

    public String getDepositType() {
        return (String) depositTypeCombo.getSelectedItem();
    }

    public String getOpenDate() {
        return openDateField.getText().trim();
    }

    public double getOpenAmount() {
        return Double.parseDouble(openAmountField.getText().trim());
    }

    public double getBalance() {
        return Double.parseDouble(balanceField.getText().trim());
    }

    public String getPassword() {
        return new String(passwordField.getPassword()).trim();
    }

    public boolean isLost() {
        return isLostCheckBox.isSelected();
    }

    public String getCustomerId() {
        return customerIdField.getText().trim();
    }
} 